$(document).ready(function() {
	UploadItem.init();
});

UploadItem = {};
UploadItem.acceptFileTypes = new Array('jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG');

UploadItem.init = function() {
	
	$('#itemUploadFileInput').change(function(e) {
		var fileInput = $(this);
		var file = $(this).val();
		if (file) {
			var fileType = UploadItem.getFileExtension(file);
			if ($.inArray(fileType, UploadItem.acceptFileTypes) > -1) {
				//File is oky...
			} else {
				$('#dialogErrorFileType').dialog({
					modal : true,
					buttons : {
						Ok : function() {
							$(this).dialog("close");
						}
					}
				});
			}
		} else {
			//No file selected...
		}
	});
	
	//Image remove
	$('.itemUploadImageFrame').bind('click', function(e){
		var id = $(this).attr('imageId')
		$("#itemUploadRemoveImageConfirmDialog").dialog({
			resizable : false,
			modal : true,
			draggable: false,
			buttons : [ {
				text : $('#itemUploadRemoveImageConfirmDialogOk').text(),
				click : function() {
					Util.redirect('/item/action/removeImage/'+id);
				}
			}, {
				text : $('#itemUploadRemoveImageConfirmDialogCancel').text(),
				click : function() {
					$(this).dialog("close");
				}
			} ]
		});
		return false;
	});

}

UploadItem.getFileExtension = function(filename) {
	var parts = filename.split('.');
	return parts[parts.length - 1];
}